'use strict';

const _ = require('lodash');

module.exports = () => {
  return async function(ctx) {
    return ctx.proxyRequest('wxmall-lv.topsports.com.cn', {
      streaming: false,
      dataType: 'json',
      contentType: 'json',
      beforeRequest: async (res) => {
      },
      beforeResponse: async (res) => {
        const originalURL = ctx.originalUrl;
        const proxyURL = '/shopCommodity/queryShopCommodityDetail/';

        if (originalURL.indexOf(proxyURL) !== -1) {
          // 如果商品状态不是 3 那么，将进入监听对接。监听事件为 3 小时
          if (res.data.data.status !== 3 && typeof res.data.data.id === 'string') {
            ctx.app.redis.hset('l:tps:shop:status', res.data.data.id, res.data.data);
          }

          res.data.data.status = 3;
          res.data.data.stock = 10;

          res.data.data.productName += res.data.data.skuList.reduce(
            (acc, item) => {
              if (item.stock === 0) {
                return acc + '';
              }

              return (
                acc +
                (item.sizeEur || item.sizeCode) +
                ' 剩余: ' +
                item.stock +
                ' ;\n '
              );
            },
            '\n'
          );
        } else if (originalURL.indexOf('/order/settle/validate') !== -1) {
          res.data = {
            code: 1,
            bizCode: 20000,
            bizMsg: '成功',
            data: null,
          };
        } else if (originalURL.indexOf('/shoppingcart/index') !== -1) {
          const data = res.data.data;
          if (data.failureNum === 0) {
            return res;
          }

          for (const item of data.failureList) {
            const updateRes = {
              code: 1,
              bizCode: 20000,
              bizMsg: '成功',
              data: {
                totalAmount: 719.1,
                totalNum: 1,
                discountAmount: 79.9,
                willBuyList: [],
                failureList: [],
                failureNum: 0,
                singleSkuLimit: 20,
                totalSkuLimit: 99,
                errorMsg: null,
              },
            };

            const fieldObj = {
              'willBuyList[0].map': {},
              'willBuyList[0].filterRules': null,
              'willBuyList[0].orderByClause': null,
              'willBuyList[0].shopId': 'shopId',
              'willBuyList[0].shopNo': 'shopNo',
              'willBuyList[0].shopName': 'shopNo',
              'willBuyList[0].totalNumber': 1,
              'willBuyList[0].logonName': null,
              'willBuyList[0].orderSource': 23,
              'willBuyList[0].virtualShopFlag': 0,
              'willBuyList[0].buyCommodityVOList': [],

              'willBuyList[0].proActivityList[0].map': {},
              'willBuyList[0].proActivityList[0].filterRules': null,
              'willBuyList[0].proActivityList[0].orderByClause': null,
              'willBuyList[0].proActivityList[0].proNo': 'proNo',
              'willBuyList[0].proActivityList[0].proName': 'proName',
              'willBuyList[0].proActivityList[0].actityType': '满折',
              'willBuyList[0].proActivityList[0].prefAmount': 0,

              'willBuyList[0].proActivityList[0].buyCommodityList[0].map': {},
              'willBuyList[0].proActivityList[0].buyCommodityList[0].filterRules':
                'filterRules',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].orderByClause':
                'orderByClause',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].shoppingcartId':
                'shoppingcartId',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].memberId':
                'memberId',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].paterId':
                'paterId',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].shopNo':
                'shopNo',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].commodityId':
                'commodityId',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].productCode':
                'productCode',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].productNo':
                'productNo',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].productName':
                'productName',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].productSizeName':
                'productSizeName',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].productSizeCode':
                'productSizeCode',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].productSizeEur':
                'productSizeEur',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].sizeNo':
                'sizeNo',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].productSkuNo':
                'productSkuNo',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].productColorName':
                'productColorName',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].productColorNo':
                'productColorNo',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].tagPrice':
                'tagPrice',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].salePrice':
                'salePrice',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].picPath':
                'picPath',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].num':
                'num',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].checked':
                'checked',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].status':
                'status',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].merchantNo':
                'merchantNo',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].brandDetailNo':
                'brandDetailNo',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].isValid':
                'isValid',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].proNo':
                'proNo',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].proName':
                'proName',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].assignProNo':
                'assignProNo',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].liveType':
                'liveType',
              'willBuyList[0].proActivityList[0].buyCommodityList[0].itemFlag':
                'itemFlag',
              totalAmount: 'tagPrice',
              totalNum: 1,
              discountAmount: 0,
              singleSkuLimit: 20,
              totalSkuLimit: 99,
              failureNum: 0,
            };

            Object.keys(fieldObj)
              .forEach((field) => {
                _.set(
                  updateRes.data,
                  field,
                  _.get(item, fieldObj[field], fieldObj[field])
                );
              });

            res.data = updateRes;
          }
        } else if (originalURL.indexOf('/shoppingcart') !== -1) {
          console.log();
        }
        return res;
      },
    });
  };
};
